package com.bms.cfcmms.model.viewsetting;

import com.bms.cfcmms.util.annotation.FieldDefine;
import com.bms.cfcmms.util.annotation.TableDefine;
import com.bms.cfcmms.model.BaseModel;
import com.bms.cfcmms.model.share.MenuTypeEnum;
import lombok.Data;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.JoinColumn;

/**
 * Created by carl on 2016/5/9.
 */
@Data
@TableDefine(group = "系统模块", primaryKey = "id", id = 9909, title = "菜单")
public class Menu extends BaseModel {

    @FieldDefine(title = "ID号", hidden = true, number = 10)
    @Column(length = 4)
    private Integer id;

    @FieldDefine(title = "父节点号", hidden = true, number = 20)
    @Column(length = 4)
    private Integer pid;

    @FieldDefine(title = "菜单内容", number = 30)
    @Column(length = 50)
    private String title;

    @FieldDefine(title = "显示内容", number = 40)
    @Column(length = 50)
    private String displayTitle;

    @FieldDefine(title = "图标文件名", remark = "图标放置于/images/module/目录下", number = 50)
    @Column(length = 50)
    private String iconUrl;

    @FieldDefine(title = "顺序号", remark = "按顺序号显示在菜单中", number = 70)
    @Column()
    private Integer orderId;

    @JoinColumn(name = "moduleId")
    @FieldDefine(title = "系统模块", number = 80)
    private com.bms.cfcmms.model.system.module.Module Module;

    @FieldDefine(title = "默认展开", number = 100)
    @Column(length = 1)
    private Boolean expand;

    private List<Menu> Menus;

    /**
     * 获取菜单类型.
     * <p/>
     * 需要将此字段menuType转换成json数据返回到前台界面去。
     *
     * @return 菜单类型的枚举MenuTypeEnum
     */
    public MenuTypeEnum getMenuType() {
        if (Menus != null && !Menus.isEmpty()) {
            return MenuTypeEnum.GROUP;
        } else if (Module != null) {
            return MenuTypeEnum.MODULE;
        }
        return MenuTypeEnum.SEPARATE;
    }

    /**
     * 获取菜单类型的模块ID.
     *
     * @return 模块ID
     */
    public Long getMenuTypeId() {
        if (Module != null) {
            return Module.getModuleId();
        }
        return null;
    }

    /**
     * 获取菜单类型的模块名称.
     *
     * @return 模块名称
     */
    public String getModuleName() {
        if (Module != null) {
            return Module.getModuleName();
        } else {
            return null;
        }
    }
}
